//
//  WSOrderCell.m
//  FD
//
//  Created by Yasin on 2018/10/15.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "WSOrderCell.h"
@interface WSOrderCell()
/**1.图片*/
@property (weak, nonatomic) IBOutlet UIImageView *iv;
/**订单分数*/
@property (weak, nonatomic) IBOutlet UILabel *countLb;
/**订单状态*/
@property (weak, nonatomic) IBOutlet UILabel *stateLB;
/**订单价格*/
@property (weak, nonatomic) IBOutlet UILabel *priceLb;


//!!!!:左右按钮
/**左边按钮 state=4时显示两个按钮*/
@property (weak, nonatomic) IBOutlet UIButton *leftBtn;
/**右边按钮
 再来一单btn @“待付款”，“立即支付”，@“退款售后”*/
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;

/**灰色的线*/
@property (weak, nonatomic) IBOutlet UIView *lightGrayLineView;
/**2019-2-21时间*/
@property (weak, nonatomic) IBOutlet UILabel *timeLb;

@end

@implementation WSOrderCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
   
    self.selectionStyle  = UITableViewCellSelectionStyleNone; //取消选中样式
    //Xcode9.1  xib 设置边框圆角 没用
    
    self.rightBtn.layer.cornerRadius = 5;
    self.rightBtn.layer.masksToBounds = YES;
    self.rightBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.rightBtn.layer.borderWidth = 1.0;
    
    
    //单独修改线条的颜色
    self.lightGrayLineView.backgroundColor = kMainLightGrayColor;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
//设置数据
- (void)setWsorderModel:(WSOrderModel *)wsorderModel {
    _wsorderModel = wsorderModel;
    
//    wsorderModel.paymentType = @(0);
//    wsorderModel.status = @(0);
    
    
    //不要占位图
    UIImage *placeholderImg = [UIImage imageNamed:@""];
    [self.iv sd_setImageWithURL:[NSURL URLWithString:wsorderModel.thumbnail] placeholderImage:placeholderImg];

    //国际化
    self.countLb.text = [NSString stringWithFormat:@"你本次订单共计%ld份餐品",(long)wsorderModel.count];
    if (![NSString rb_ChineseLanguage]) {
        self.countLb.text = [NSString stringWithFormat:@"Total Order: %ld item",(long)wsorderModel.count];
    }
    
    //2019-2-26 保留1位小数点，跟android保持一致
    self.priceLb.text = [NSString stringWithFormat:@"€%.1f",[wsorderModel.actualAmount floatValue]]; 
    

    
    //2019-2-21时间
    self.timeLb.text = [self timeStrFromTimeStamp:wsorderModel.createTime];
    
    
    //设置右上角的 “待付款、待评价、退款、售后”lb文字标题 2019-1-21
    [self setupStateLBWithState:wsorderModel.status];

    //设置右下角按钮点击样式
    [self setupLeftRightBtnDisplayLogicWithPaymentType:wsorderModel.paymentType stateNum:wsorderModel.status];
}

- (NSString *)timeStrFromTimeStamp:(NSNumber *)timestamp {
    NSTimeInterval interval = [timestamp floatValue] / 1000.0;
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setDateFormat:@"yyyy-MM-dd"];
    
    return [fmt stringFromDate:date];
}


//!!!!:右上角订单状态显示逻辑
- (void)setupStateLBWithState:(NSNumber *)stateNum {
//deliveryType=0 配送方式为外送
    if ([stateNum isEqualToNumber:@(0)]) {
        self.stateLB.text = NSLocalizedString(Order_Pending, @"待付款");
    }else if([stateNum isEqualToNumber:@(1)]) {
        self.stateLB.text = NSLocalizedString(L_Order_YiXiaDan,@"已下单");
    }else if ([stateNum isEqualToNumber:@(2)]) {
        self.stateLB.text = NSLocalizedString(L_Order_ZhiZuoZhong, @"制作中");
    }else if ([stateNum isEqualToNumber:@(3)]) {
        self.stateLB.text = NSLocalizedString(L_Order_PaiSongZhong, @"派送中");
    }else if ([stateNum isEqualToNumber:@(4)]) {
        self.stateLB.text = NSLocalizedString(Order_Evaluate, @"待评价");
    }else if ([stateNum isEqualToNumber:@(5)]) {
        self.stateLB.text = NSLocalizedString(Evaluated, @"已评价");
    }else if ([stateNum isEqualToNumber:@(6)]) {
        self.stateLB.text = NSLocalizedString(L_Order_ShenQinTuikuanZhong, @"申请退款中");
    }else if ([stateNum isEqualToNumber:@(7)]) {
        self.stateLB.text = NSLocalizedString(L_Order_TuiKuanChuLiZhong, @"退款处理中");
    }else if ([stateNum isEqualToNumber:@(8)] || [stateNum isEqualToNumber:@(9)]) {
        self.stateLB.text = NSLocalizedString(L_Order_Closed, @"订单关闭");
    }
}

//!!!!:右下角按钮显示状态:
//0.什么时候显示2个按钮,什么时候只显示一个
//1.什么时候显示"再来一单" 、“立即支付”
- (void)setupLeftRightBtnDisplayLogicWithPaymentType:(NSNumber *)paymentType stateNum:(NSNumber *)stateNum {
    NSString *zlydStr = NSLocalizedString(Order_OrderAgain, @"再来一单");
    
    //0.什么时候显示两个按钮(@"再来一单、评价")
    if ([stateNum isEqualToNumber:@(4)]) {
        self.leftBtn.hidden = NO; //显示
        [self.leftBtn setTitle:zlydStr forState:(UIControlStateNormal)];
        [self lightGrayButton:self.leftBtn]; //浅灰
        
        
        [self.rightBtn setTitle:NSLocalizedString(L_Order_Evaluate, @"评价") forState:UIControlStateNormal];
        [self lightRedButton:self.rightBtn]; //浅红
    }else {
        self.leftBtn.hidden = YES; //只保留一个按钮
    }
    
    //@“再来一单”
    if ([stateNum isEqualToNumber:@(5)] ||
        [stateNum isEqualToNumber:@(6)] ||
        [stateNum isEqualToNumber:@(7)] ||
        [stateNum isEqualToNumber:@(8)] ||
        [stateNum isEqualToNumber:@(9)]) {
        [self.rightBtn setTitle:zlydStr forState:(UIControlStateNormal)];
        [self lightGrayButton:self.rightBtn];
    }
    //2019-2-18 payment == 0 && status=3 再来一单
    if ([paymentType isEqualToNumber:@(0)] &&
        [stateNum isEqualToNumber:@(3)]) {
        [self.rightBtn setTitle:zlydStr forState:(UIControlStateNormal)];
        [self lightGrayButton:self.rightBtn];
    }
    
    //2019-2-20 status= 1、 2、3 '再来一单'
    if ([stateNum isEqualToNumber:@(1)] ||
        [stateNum isEqualToNumber:@(2)]) {
        [self.rightBtn setTitle:zlydStr forState:(UIControlStateNormal)];
        [self lightGrayButton:self.rightBtn];
    }
    
    
    
    
    
    
    
    
    //@“立即支付”
    NSString *ljzfStr = NSLocalizedString(Order_PayNow, @"立即支付");
    if ([paymentType isEqualToNumber:@(0)] &&
        [stateNum isEqualToNumber:@(0)]) {
        [self.rightBtn setTitle:ljzfStr forState:(UIControlStateNormal)];
        [self lightRedButton:self.rightBtn]; //更新按钮文字颜色
    }
    
    if ([paymentType isEqualToNumber:@(1)]) {
        if ([stateNum isEqualToNumber:@(0)] ||
            [stateNum isEqualToNumber:@(1)] ||
            [stateNum isEqualToNumber:@(2)] ||
            [stateNum isEqualToNumber:@(3)]) {
            [self.rightBtn setTitle:ljzfStr forState:(UIControlStateNormal)];
            [self lightRedButton:self.rightBtn]; //更新按钮文字颜色
        }
    }
}





/**更新按钮文字状态 */
- (void)lightGrayButton:(UIButton *)button {
    
    //灰色：999999
    UIColor *hui = [UIColor rb_colorHex:0x999999];
    [button setTitleColor:hui forState:(UIControlStateNormal)];
    button.layer.borderColor = hui.CGColor;
}

- (void)lightRedButton:(UIButton *)button {
    //红色：e54b4b
    UIColor *r = [UIColor rb_colorHex:0xe54b4b];
    [button setTitleColor:r forState:(UIControlStateNormal)];
    button.layer.borderColor = r.CGColor;
}

#pragma mark - 12-4按钮点击事件

//左
- (IBAction)clickLeftBtnAction:(UIButton *)sender {
    if (self.clickLeftBtnBlcok) {
        self.clickLeftBtnBlcok(self.wsorderModel.status, self.wsorderModel.paymentType);
    }
}

//右
- (IBAction)clickRightBtnAction:(UIButton *)sender {
    if (self.clickRightBtnBlock) {
        self.clickRightBtnBlock(self.wsorderModel.status, self.wsorderModel.paymentType);
    }
}

//时间戳转时间
- (NSString *)timeFromTimeStamp:(NSNumber *)timeStamp {
 
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    [fmt setDateFormat:@"yyyy-MM-dd"];
    //1546933536000
    NSTimeInterval interval = [timeStamp doubleValue] / 1000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSString *timeStr = [fmt stringFromDate:date];
    return timeStr;
}

@end
